package com.zero.clinic.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zero.clinic.pojo.*;
import com.zero.clinic.service.*;
import com.zero.clinic.utils.Constant;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/patientDiagnose")
public class PatientDiagnoseController {
    @Resource
    private PatientDiagnoseService patientDiagnoseService;
    @Resource
    private PatientRegisterService patientRegisterService;
    @Resource
    private RecipeService recipeService;

    /**
     * @function 进入就诊管理模块
     * @return
     */
    @RequiresPermissions("patientDiagnose:patientDiagnosePage")
    @RequestMapping("/patientDiagnosePage")
    public String patientDiagnosePage(){
        return "patientDiagnosePage";
    }

    /**
     * @function 就诊列表
     * @param pageNum
     * @param pageSize
     * @param keywords
     * @return
     */
    @ResponseBody
    @RequiresPermissions("patientDiagnose:list")
    @RequestMapping("/list")
    public PageInfo<PatientDiagnose> patientDiagnoseList(@RequestParam(defaultValue = "1") int pageNum,
                                   @RequestParam(defaultValue = "10") int pageSize, String keywords){
        PageHelper.startPage(pageNum, pageSize);
        // 1.查出所有的PatientDiagnose信息
        PatientDiagnoseExample example = new PatientDiagnoseExample();
        // 2 根据搜索关键词查询
        if (StringUtils.isNotBlank(keywords)){
            // 2.1 根据根患者姓名查询
            PatientDiagnoseExample.Criteria criteria1 = example.createCriteria();
            criteria1.andPatientNameLike("%"+ keywords +"%");
            // 2.2 根据根患者性别查询
            PatientDiagnoseExample.Criteria criteria2 = example.or();
            if (keywords.equals("男")) {
                criteria2.andPatientGenderEqualTo(1L);
            } else if (keywords.equals("女")) {
                criteria2.andPatientGenderEqualTo(2L);
            }
            // 2.3 根据患者地址查询
            PatientDiagnoseExample.Criteria criteria3 = example.or();
            criteria3.andPatientAddressLike("%"+ keywords +"%");
            // 2.4 根据挂号科室查询
            PatientDiagnoseExample.Criteria criteria4 = example.or();
            criteria4.andRegisterDeptLike("%"+ keywords +"%");
            // 2.5 根据主治医师姓名查询
            PatientDiagnoseExample.Criteria criteria5 = example.or();
            criteria5.andDoctorLike("%"+ keywords +"%");
            // 2.6 根据处方查询
            PatientDiagnoseExample.Criteria criteria6 = example.or();
            criteria6.andRecipeNameLike("%"+ keywords +"%");
            // 2.7 根据处方类型查询
            PatientDiagnoseExample.Criteria criteria7 = example.or();
            criteria7.andRecipeTypeLike("%"+ keywords +"%");
        }
        List<PatientDiagnose> patientDiagnoses = patientDiagnoseService.selectByExample(example);
        // 3.以PageInfo对象返回给前端
        PageInfo<PatientDiagnose> pageInfo = new PageInfo<>(patientDiagnoses);
        return pageInfo;
    }

    /**
     * @function 编辑就诊页面
     * @param diagnoseId
     * @param m
     * @return
     */
    @RequestMapping("/edit")
    public String patientDiagnoseEdit(Long diagnoseId, Model m){
        // 1.如果前端页面需要更新就诊信息，则会传一个需要更新的diagnoseId过来。这里需要通过diagnoseId查出更新信息后共享给前端页面
        if (diagnoseId != null) {
            PatientDiagnose patientDiagnose = patientDiagnoseService.selectByPrimaryKey(diagnoseId);
            Date patientBirthday = patientDiagnose.getPatientBirthday();
            patientDiagnose.setPatientBirthdayStr(new SimpleDateFormat("yyyy-MM-dd").format(patientBirthday));
            Date symptomEmergeTime = patientDiagnose.getSymptomEmergeTime();
            patientDiagnose.setSymptomEmergeTimeStr(new SimpleDateFormat("yyyy-MM-dd").format(symptomEmergeTime));
            System.out.println("当前操作的就诊信息：" + patientDiagnose);
            m.addAttribute("patientDiagnose",patientDiagnose);
        }

        // 2.将所有status=0(代表未就诊)的门诊挂号信息共享给前端
        PatientRegisterExample patientRegisterExample = new PatientRegisterExample();
        PatientRegisterExample.Criteria criteria = patientRegisterExample.createCriteria();
        criteria.andStatusEqualTo(0L);
        List<PatientRegister> patientRegisters = patientRegisterService.selectByExample(patientRegisterExample);
        m.addAttribute("patientRegisters", patientRegisters);

        // 3.将所有的处方信息共享给前端
        RecipeExample recipeExample = new RecipeExample();
        List<Recipe> recipes = recipeService.selectByExample(recipeExample);
        m.addAttribute("recipes",recipes);


        return "patientDiagnoseEdit";
    }

    /**
     * @function 新增就诊
     * @param patientDiagnose
     * @return
     */
    @ResponseBody
    @RequestMapping("/insert")
    @RequiresPermissions("patientDiagnose:insert")
    public MessageObject patientDiagnoseInsert(@RequestBody PatientDiagnose patientDiagnose){
        System.out.println("需要新增的就诊：" + patientDiagnose);
        //1.插入数据
        int row = patientDiagnoseService.insert(patientDiagnose);
        if (row > 0) {
            return MessageObject.createMo(1,"新增数据成功");
        }
        return MessageObject.createMo(0,"新增数据失败，请联系技术人员");
    }

    /**
     * @function 更新就诊
     * @param patientDiagnose
     * @return
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("patientDiagnose:update")
    public MessageObject patientDiagnoseUpdate(@RequestBody PatientDiagnose patientDiagnose){
        //1.更新数据
        System.out.println("需要更新的就诊：" + patientDiagnose);
        int row = patientDiagnoseService.updateByPrimaryKeySelective(patientDiagnose);
        if (row > 0) {
            return MessageObject.createMo(1,"更新数据成功");
        }
        return MessageObject.createMo(0,"更新数据失败，请联系技术人员");
    }

    /**
     * @function 显示就诊详情
     * @param diagnoseId
     * @param m
     * @return
     */
    @RequestMapping("/detail")
    @RequiresPermissions("patientDiagnose:detail")
    public String patientDiagnoseDetail(Long diagnoseId, Model m){
        // 1.如果前端页面需要显示就诊详情，则会传一个需要显示的diagnoseId过来。这里需要通过diagnoseId查出详细信息后共享给前端页面
        if (diagnoseId != null) {
            PatientDiagnose patientDiagnose = patientDiagnoseService.selectByPrimaryKey(diagnoseId);
            Date patientBirthday = patientDiagnose.getPatientBirthday();
            patientDiagnose.setPatientBirthdayStr(new SimpleDateFormat("yyyy-MM-dd").format(patientBirthday));
            Date symptomEmergeTime = patientDiagnose.getSymptomEmergeTime();
            patientDiagnose.setSymptomEmergeTimeStr(new SimpleDateFormat("yyyy-MM-dd").format(symptomEmergeTime));
            System.out.println("需要显示详情的就诊信息：" + patientDiagnose);
            m.addAttribute("patientDiagnose",patientDiagnose);
        }



        // 3.将所有的处方信息共享给前端
        RecipeExample recipeExample = new RecipeExample();
        List<Recipe> recipes = recipeService.selectByExample(recipeExample);
        m.addAttribute("recipes",recipes);

        return "patientDiagnoseDetail";
    }

    /**
     * @function 删除就诊
     * @param diagnoseId
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete")
    @RequiresPermissions("patientDiagnose:delete")
    public MessageObject patientDiagnoseDelete(Long diagnoseId){
        System.out.println("需要删除的就诊ID: " + diagnoseId);
        //1.根据diagnoseId删除数据
        int row = patientDiagnoseService.deleteByPrimaryKey(diagnoseId);
        if (row > 0){
            return MessageObject.createMo(1,"删除数据成功");
        }
        return MessageObject.createMo(0,"删除数据失败，请联系技术人员");
    }

    /**
     * @function 批量删除就诊
     * @param diagnoseIds
     * @return
     */
    @ResponseBody
    @RequestMapping("/batchdel")
    @RequiresPermissions("patientDiagnose:batchdel")
    public MessageObject patientDiagnoseBatchDelete(String diagnoseIds){
        String[] diagnoseIdsArr = diagnoseIds.split(",");
        //2.根据diagnoseIdsArr删除数据
        int row = patientDiagnoseService.deleteByIds(diagnoseIdsArr);
        if (row > 0){
            return MessageObject.createMo(1,"删除数据成功");
        }
        return MessageObject.createMo(0,"删除数据失败");
    }
}